//
//  MBHomeHeaderView.m
//  MemberBusiness-ios
//
//  Created by cherry pu on 2018/4/23.
//  Copyright © 2018年 zhangXin. All rights reserved.
//

#import "MBHomeHeaderView.h"


@implementation MBHomeHeaderView

- (instancetype)init{
    self = [super init];
    if (self) {
        CAGradientLayer *gradientLayer0 = [[CAGradientLayer alloc] init];
        gradientLayer0.frame = CGRectMake(0, 0, SCREEN_WIDTH, [UIView lf_sizeFromIphone6:69]);
        gradientLayer0.colors = @[
                                  (id)[UIColor colorWithRed:255.0f/255.0f green:136.0f/255.0f blue:0.0f/255.0f alpha:1.0f].CGColor,
                                  (id)[UIColor colorWithRed:255.0f/255.0f green:99.0f/255.0f blue:0.0f/255.0f alpha:1.0f].CGColor];
        gradientLayer0.locations = @[@0, @1];
        [gradientLayer0 setStartPoint:CGPointMake(0, 0)];
        [gradientLayer0 setEndPoint:CGPointMake(1, 0)];
        [self.layer addSublayer:gradientLayer0];
        
        MBLoginUserModel* userModel = [MBLoginUserModelArchieveTool userInfoUnAchieveFromFile];

        UIImageView* headerImageView = [[UIImageView alloc]init];
        [self addSubview:headerImageView];
        
        [headerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset([UIView lf_sizeFromIphone6:18]);
            make.left.equalTo(self).offset([UIView lf_sizeFromIphone6:12]);
            make.size.mas_equalTo(CGSizeMake([UIView lf_sizeFromIphone6:34], [UIView lf_sizeFromIphone6:34]));
        }];
        
        NSString* imageUrl = userModel.head_pic;
        if (![imageUrl containsString:@"http"]) {
            imageUrl = [NSString stringWithFormat:@"%@%@",BASE_IMAGE_URL,userModel.head_pic];
        }
        
        UIImage* header = [[MBCacheService sharedInstance]fetchHeaderWithHeaderName:userModel.head_pic];
        if (header) {
            headerImageView.image = header;
        }else {
            [headerImageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage scaleImage:[UIImage imageNamed:@"moren"] toSize:CGSizeMake([UIView lf_sizeFromIphone6:34], [UIView lf_sizeFromIphone6:34])] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                [[MBCacheService sharedInstance]cacheHeaderWithHeaderName:userModel.head_pic withHeader:image];
            }];
        }
        
        UILabel* nameLabel = [[UILabel alloc]init];
        nameLabel.text = [NSString stringWithFormat:@"%@，欢迎您",userModel.seller_name];
        nameLabel.font = [UIFont lf_mediumFontOfSize:17];
        nameLabel.textColor = [UIColor whiteColor];
        [self addSubview:nameLabel];
        
        [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(headerImageView.mas_right).offset([UIView lf_sizeFromIphone6:11]);
            make.bottom.equalTo(headerImageView.mas_centerY).offset([UIView lf_sizeFromIphone6:-3.5]);
            make.width.mas_lessThanOrEqualTo([UIView lf_sizeFromIphone6:150]);
        }];

        UILabel* companyLabel = [[UILabel alloc]init];
        companyLabel.text = userModel.store_name;
        companyLabel.font = [UIFont lf_systemFontOfSize:12];
        companyLabel.textColor = [UIColor whiteColor];
        [self addSubview:companyLabel];
        
        [companyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(nameLabel);
            make.top.equalTo(headerImageView.mas_centerY).offset([UIView lf_sizeFromIphone6:3.5]);
            make.width.mas_lessThanOrEqualTo([UIView lf_sizeFromIphone6:150]);
        }];

        MBTopCenterButton* scanButton = [[MBTopCenterButton alloc]init];
        self.scanButton = scanButton;
        scanButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [scanButton setImage:[UIImage imageNamed:@"home_scan"] forState:UIControlStateNormal];
        [scanButton setTitle:@"二维码核销" forState:UIControlStateNormal];
        scanButton.titleLabel.font = [UIFont lf_systemFontOfSize:12];
        scanButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:scanButton];
        
        [scanButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset([UIView lf_sizeFromIphone6:-12]);
            make.bottom.equalTo(self).offset([UIView lf_sizeFromIphone6:-15]);
            make.size.mas_equalTo(CGSizeMake([UIView lf_sizeFromIphone6:65], [UIView lf_sizeFromIphone6:42]));
        }];
        
        MBTopCenterButton* scanNumButton = [[MBTopCenterButton alloc]init];
        self.scanNumButton = scanNumButton;
        scanNumButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [scanNumButton setImage:[UIImage imageNamed:@"home_scan_num"] forState:UIControlStateNormal];
        [scanNumButton setTitle:@"验证码核销" forState:UIControlStateNormal];
        scanNumButton.titleLabel.font = [UIFont lf_systemFontOfSize:12];
        scanNumButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:scanNumButton];
        
        [scanNumButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(scanButton.mas_left).offset([UIView lf_sizeFromIphone6:-15]);
            make.bottom.equalTo(self).offset([UIView lf_sizeFromIphone6:-15]);
            make.size.mas_equalTo(CGSizeMake([UIView lf_sizeFromIphone6:65], [UIView lf_sizeFromIphone6:42]));
        }];
    }
    return self;
}

@end
